package com.java.atm.Server;

import com.java.atm.Account.User;

import java.io.*;
import java.net.Socket;
import java.sql.*;
import java.sql.Date;
import java.util.*;

public class ATM
{

    private PrintWriter out;
    private BufferedReader in;
    public User currentUser;
    private Connection connection;
    private Statement statement;
    private Socket socket;
    private PreparedStatement psmt;
    Scanner scanner;


    public ATM(Socket socket, Connection connection)throws SQLException,IOException
    {
        //初始化需要的所有参数
        String sql = "INSERT INTO user(user_name, password)VALUES(?,?)";
        scanner = new Scanner(System.in);
        currentUser = null;
        this.connection = connection;
        this.statement = connection.createStatement();
        this.psmt = connection.prepareStatement(sql);
        //this.socket = socket;
        this.connection = connection;
        //in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
        currentUser = null;

    }
    public void SignUp()throws IOException,SQLException
    {

        String UserName = null;
        while(UserName.equals(null))
        {
            UserName = in.readLine();
        }
        String pass1 = null;
        while(pass1.equals(null))
        {
            pass1 = in.readLine();
        }


        //将注册用户的信息写入数据库

        Integer passHash = pass1.hashCode();
        psmt.setString(1, UserName);
        psmt.setString(2, passHash.toString());
        psmt.addBatch();
        psmt.clearParameters();
        psmt.executeBatch();
        psmt.clearBatch();
        out.println(21);
        String sql = "SELECT id FORM user";
        ResultSet rs = statement.executeQuery(sql);
        rs.last();
        int userId = rs.getRow();
        out.println("Your user id is: " + userId + 1);


    }

    public boolean login()
    {
        int userId = 0;
        while(userId == 0)
        {
            try
            {
                userId = in.read();
            }catch(Exception e)
            {

            }

        }
        String sql = "SELECT*FROM user WHERE id=" + userId;
        try
        {
            ResultSet rs = statement.executeQuery(sql);
            rs.last();
            if(rs.getString("id") != null)
            {
                //在数据库中找到对象

                String pass = null;
                while(pass.equals(null))
                {
                    try
                    {
                        pass = in.readLine();
                    }catch(Exception e)
                    {

                    }

                }

                Integer password = pass.hashCode();

                if(rs.getString("password").equals(password.toString()))
                {
                    //登陆成功
                    currentUser = new User(rs);
                    out.println(11);//向客户端发送成功代码
                    return true;
                }else
                {
                    out.println(10);//向客户端发送失败代码
                }


            }else
            {
                out.println(10);
            }
        }catch(SQLException e)
        {
            e.getErrorCode();
        }
        return false;

    }

    public void checkDeposit()
    {
        try
        {
            double amount = 0;
            while(amount == 0)
            {
                amount = in.read();
            }
            currentUser.checkAccount.balance += amount;

            out.println(31);

        }catch(Exception e)
        {

        }

    }
    public void halfDeposit()
    {
        try
        {
            double amount = 0;
            while(amount == 0)
            {
                amount = in.read();
            }
            currentUser.savingAccount.hya.balance += amount;
            out.println(31);
        }catch(Exception e)
        {

        }
    }

    public void oneDeposit()
    {
        try
        {
            double amount = 0;
            while(amount == 0)
            {
                amount = in.read();
            }
            currentUser.savingAccount.oya.balance += amount;
            out.println(31);
        }catch(Exception e)
        {

        }

    }

    public void fiveDeposit()
    {
        try
        {
            double amount = 0;
            while(amount == 0)
            {
                amount = in.read();
            }
            currentUser.savingAccount.fya.balance += amount;
            out.println(31);
        }catch(Exception e)
        {

        }
    }

    public void checkWithdarl()
    {
        try
        {
            int amount = 0;
            while(amount == 0)
            {
                amount = in.read();
            }
            if(currentUser.checkAccount.balance - amount >= 0)
            {
                currentUser.checkAccount.balance -= amount;
                currentUser.checkAccount.lastTransion = new Date(System.currentTimeMillis());
                out.println(41);
            }else
            {
                out.println(40);
            }

        }catch(Exception e)
        {
            e.getStackTrace();
        }

    }
    public void halfWithdal()
    {
        try
        {
            int amount = 0;
            while(amount == 0)
            {
                amount = in.read();
            }
            if(currentUser.savingAccount.hya.balance - amount >= 0)
            {
                currentUser.savingAccount.hya.balance -= amount;
                currentUser.savingAccount.hya.lastTransion = new Date(System.currentTimeMillis());
                out.println(41);
            }else
            {
                out.println(40);
            }

        }catch(Exception e)
        {
            e.getStackTrace();
        }

    }

    public void oneWithdal()
    {
        try
        {
            int amount = 0;
            while(amount == 0)
            {
                amount = in.read();
            }
            if(currentUser.savingAccount.oya.balance - amount >= 0)
            {
                currentUser.savingAccount.oya.balance -= amount;
                currentUser.savingAccount.oya.lastTransion = new Date(System.currentTimeMillis());
                out.println(41);
            }else
            {
                out.println(40);
            }

        }catch(Exception e)
        {
            e.getStackTrace();
        }
    }
    public void fiveWithdal()
    {

        try
        {
            int amount = 0;
            while(amount == 0)
            {
                amount = in.read();
            }
            if(currentUser.savingAccount.fya.balance - amount >= 0)
            {
                currentUser.savingAccount.fya.balance -= amount;
                currentUser.savingAccount.fya.lastTransion = new Date(System.currentTimeMillis());
                out.println(41);
            }else
            {
                out.println(40);
            }

        }catch(Exception e)
        {
            e.getStackTrace();
        }
    }


    public void logout()throws IOException
    {
        //将当前用户的信息保存回数据库
        String sql = "UPDATE user SET balance=?,obalance=?,hbalance=?,fbalance=?,lst=?,lost=?,lhst=?,lhst=?,lfst=? WHERE id=?";
        try
        {
            PreparedStatement psmt = connection.prepareStatement(sql);
            psmt.setDouble(1, currentUser.checkAccount.getBalance());
            psmt.setDouble(2, currentUser.savingAccount.oya.getBalance());
            psmt.setDouble(3, currentUser.savingAccount.hya.getBalance());
            psmt.setDouble(4, currentUser.savingAccount.fya.getBalance());
            psmt.setDate(5, new Date(currentUser.checkAccount.getLastTransion().getTime()));
            psmt.setDate(6, new Date(currentUser.savingAccount.oya.getLastTransion().getTime()));
            psmt.setDate(7, new Date(currentUser.savingAccount.hya.getLastTransion().getTime()));
            psmt.setDate(8, new Date(currentUser.savingAccount.fya.getLastTransion().getTime()));
            psmt.setInt(9, currentUser.getUserId());
            psmt.addBatch();
            psmt.clearParameters();
            psmt.executeBatch();
            psmt.clearBatch();
            currentUser = null;
            connection.close();
            psmt.close();
            this.psmt.close();
            statement.close();
            in.close();
            out.close();
            socket.close();
        }catch(SQLException e)
        {
            e.getErrorCode();
        }




    }


    /*

    public void StartBusiness()throws IOException
    {


        PrintWelcome();
        int opt = printMenu();

        while (opt != 6)
        {
            switch (opt)
            {
                case 1:
                    login();
                    opt = printMenu();
                    break;
                case 2:
                    SignUp();
                    opt = printMenu();
                    break;
                case 3:
                    deposit();
                    opt = printMenu();
                    break;
                case 4:
                    withdrawl();
                    opt = printMenu();
                    break;
                case 5:
                    getBalance();
                    opt = printMenu();
                    break;


                default:
                    System.out.println("invalid option!!!!");
                    opt = printMenu();
                    break;
            }

        }

        System.out.println("Thank you for using, " + currentUser.getUserName() + ", Good Bye!!");
        logout();

        StartBusiness();




    }

    public User findA*/

}
